// components/foodRecord/foodRecord.js
import fetch from "../../utils/fetch";
const globalData = getApp().globalData;
const userId = globalData.userInfo.id;
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    selected: {
      value: {},
      type: Object
    },
    gram: {
      value: 100,
      type: Number,
    },
    gramCurrentNumber: {
      type: Number,
    },
    totalHeat: {
      type: Number
    },
    type: "food",
    unitHeat: {
      type: Number
    },
    recordDate: {
      type: String
    },
    foodRecord: {
      type: Object
    },
    time: {
      type: String
    }

  },

  /**
   * 组件的初始数据
   */
  data: {
    recordSport: []
  },

  /**
   * 组件生命周期
   */
  lifetimes: {
    //  attached:function(){
    //     const index = this.data.timeArr.indexOf(this.data.foodRecord.time);
    //     this.setData({
    //       index
    //     })
    //  }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    //前往详情
    gotoFoodDetail(e) {
      const foodId = e.currentTarget.dataset.foodId;
      if (type == 'food') {
        wx.navigateTo({
          url: `../foodDetail/foodDetail?foodId=${foodId}&recordDate=${this.data.recordDate}&time=${this.data.time}`,
        })
      }
    },
    getCurrentNumbergram(e) { //克数
      let result = e.detail.current;
      const type = this.data.type;
      let totalHeat;
      if (type == 'food') {
        totalHeat = parseFloat(this.data.unitHeat * (result / 100)).toFixed(2);
      } else {
        totalHeat = parseFloat(this.data.unitHeat * (result / 30)).toFixed(2);
      }

      this.setData({
        gram: result,
        gramCurrentNumber: result,
        totalHeat: totalHeat
      })
    },
    add() {
      if (this.data.type == 'food') {
        const food = {};
        food.time = this.data.time;
        food.num = this.data.gram;
        food.heat = parseFloat(this.data.totalHeat);
        food.userId = userId;
        console.log(this.data.recordDate);
        food.recordDate = this.data.recordDate;
        const {
          id,
          img,
          name,
          carbohydrate,
          protein,
          fat
        } = this.data.selected;
        food.foodId = id;
        food.name = name;
        food.img = img;
        const temp = parseFloat(this.data.gram / 100);
        food.carbohydrate = parseFloat(carbohydrate * temp).toFixed(2);
        food.protein = parseFloat(protein * temp).toFixed(2);
        food.fat = parseFloat(fat * temp).toFixed(2);
        const foodRecord = this.data.foodRecord;
        foodRecord.push(food);
        this.setData({
          foodRecord,
        });
        this.triggerEvent('onClose', {
          foodRecord
        });
      } else {
        // delete selectedSport.id;
        const selectedSport = this.data.selected;
        selectedSport.userId = userId;
        selectedSport.sportsId = selectedSport.id;
        selectedSport.totalHeat = this.data.totalHeat;
        selectedSport.num = this.data.gramCurrentNumber;
        selectedSport.recordDate = this.data.recordDate;
        const recordSport = this.data.recordSport;
        recordSport.push(selectedSport);
        this.triggerEvent('onClose', {
          recordSport
        });
      }
    },
    //阻断冒泡
    stopProgation: function () {}
  }
})